#define _CRT_SECURE_NO_WARNINGS 1

#include"string.h"

namespace word
{
	typedef char* iterator;
	string::string(const string& str)
	{
		_size = str._size;
		_capacity = str._capacity;
		char* tmp = new char[str._capacity + 1];
		strcpy(tmp, str._str);
	}

	string& string::reserve(size_t n)
	{
		if (n > _capacity)
		{
			char* tmp = new char[n + 1];
			_capacity = n;
			strcpy(tmp, _str);
			delete[]_str;
			_str = tmp;
		}
		return (*this);
	}

	string& string::push_back(char ch)
	{
		if (_size == _capacity)
		{
			reserve(_capacity == 0 ? 4 : 2 * _capacity);
		}
		_str[_size] = ch;
		_str[++_size] = '\0';
		return (*this);
	}

	string& string::append(const char* str)
	{
		size_t len = strlen(str);
		if (len + _size > _capacity)
		{
			reserve(len + _size > 2 * _capacity ? len + _size + 1 : 2 * _capacity);
		}
		strcpy(_str + _size, str);
		_size += len;
		return (*this);
	}

	const char* string::c_str()
	{
		return _str;
	}

	iterator string::begin()
	{
		return _str;
	}

	iterator string::end()
	{
		return (_str + _size);
	}



	std::ostream& operator << (std::ostream& out,const string& str)
	{
		for (char e : str.begin())
		{
			out << e;
		}
		return out;
	}

	std::istream& operator >> (std::istream& out, string& str);

	void test_string()
	{
		//string s1("abcd");
		//string s2;
		//std::cout << s1.c_str() << std::endl;
		//std::cout << s2.c_str() << std::endl;
		//s2.append("abcd");
		//std::cout << s2.c_str() << std::endl;
		//s1.push_back('a');
		//std::cout << s1.c_str() << std::endl;
		string s1("abcdef");
		std::cout << s1 << std::endl;
	}
}